/**
 * Created by Administrator on 2017/9/26.
 */

//动态创建购物车中的商品列表
$(function(){
    //创建表格
    var oTab = $("<table>" +
            //表头
        "<thead>" +
        "<tr>" +
        "<th>" +
        "<h1>母婴之家商城</h1><h3>母婴之家商城购物车</h3>" +
        "</th>" +
        "<th>" +
        "单价（元）" +
        "</th>"+
        "<th>数量（件）</th>" +
        "<th>小计（元）</th>" +
        "<th>操作</th>"+
        "</tr>" +
        "</thead>" +
        "</table>");

    $("#main").append(oTab);

    var str = $.cookie("shopThing");
    console.log(str);
    //用于防止商品总价格
    var mount = 0;
    if(str){
        var strObj = JSON.parse(str);
        var oTbody = $("<tbody></tbody>");
        $("#main>table").append(oTbody);

        //创建动态的商品列表
        for(var i = 0; i < strObj.length; i++){

            var numCon = strObj[i].number;
            var sum = strObj[i].number * strObj[i].doll;
            console.log(sum);

            var oTr = "<tr>" +
                "<td class='tdh2'>" +
                "<span>" +
                "<img src='" + strObj[i].shopImg + "' alt=''>" +
                "</span><h2>" + strObj[i].h1 + "</h2>" +
                "</td>" +
                "<td id='dj'>" + strObj[i].doll + "</td>" +
                "<td>" +
                "<span class='td_left'>-</span>" +
                "<input type='text' class='cou'/>" +
                "<span class='td_right'>+</span>" +
                "</td>" +
                "<td id='zh'>" + sum + "</td>" +
                "<td>" +
                "<a href='javascript:void(0)' class='deleobj'>删除</a>" +
                "</td>" +
                "</tr>";
            console.log(numCon);
            $("#main>table>tbody").append(oTr);
            $(".cou").eq(i).val(numCon);

            mount +=Number(sum);


        }



        //创建动态的商品结算列表
        var oFoot = "<div id='list'>" +
            "<a href='#'>清空购物车</a>" +
            "<p>总计（不含运费）：</p>" +
            "<i>" + mount.toFixed(2) + "</i>" +
            "<div>确认订单</div></div>"
        $("#main").append(oFoot);

        //点击设置商品数量的加
        $(".td_right").on("click", function(){
            var count = parseInt($(this).siblings("input").val());
            $(this).siblings("input").val(count + 1);
            var zhCount = Number($(this).parent("td").siblings("#zh").text())+Number($(this).parent("td").siblings("#dj").text());
            $(this).parent("td").siblings("#zh").text(zhCount.toFixed(2));

            var zhMount = Number($("#list").children("i").text())+Number($(this).parent("td").siblings("#dj").text());
            //每次点击添加商品的时候，对应的商品数量总和也是应该增加的
            $("#list").children("i").text(zhMount.toFixed(2));
        })

        //点击设置商品数量的减
        $(".td_left").on("click", function(){
            var count = parseInt($(this).siblings("input").val());
            $(this).siblings("input").val(count - 1);
            if(count<=0){
                $(this).siblings("input").val(0);
            }

            var zhCount = Number($(this).parent("td").siblings("#zh").text())-Number($(this).parent("td").siblings("#dj").text());
            $(this).parent("td").siblings("#zh").text(zhCount.toFixed(2));

            //每次点击-，对应的商品数量总和也是应该减一的
            var zhMount = Number($("#list").children("i").text())-Number($(this).parent("td").siblings("#dj").text());
            $("#list").children("i").text(zhMount.toFixed(2));
        })


        //点击删除，删除页面效果和cookie数据
                $(".deleobj").on("click",function () {
                    var idstr1 = $(this).parents("td").siblings(".tdh2").find("h2").text();
                    //删除页面效果
                    $(this).parents("td").parents("tr").remove();
                    //删除cookie总和
                    var jsonstr = $.cookie("shopThing");
                    var newarr = [];
                    if (jsonstr) {
                        console.log(jsonstr);
                        var jsonobj = JSON.parse(jsonstr);
                        for (var i = 0; i < jsonobj.length; i++) {
                            if (jsonobj[i].h1 == idstr1) {
                                continue;
                            }else{
                                newarr.push(jsonobj[i]);
                            }
                        }
                        console.log(newarr);
                        if (newarr.length<=0) {
                            $.cookie("shopThing","",{expires:-1});
                        }else{
                            $.cookie("shopThing",JSON.stringify(newarr),{expires:30});
                        }
                    }
                })

    }
})